// TODO set tpl cache.
// TODO more path setup and error process.
dojo.provide("lotex.render");

dojo.require("dojo.lang.declare");
dojo.require("dojo.event.*");
dojo.require("lotex.system.AsynControler");
dojo.require("lotex.string");
dojo.require("lotex.io");

dojo.declare("lotex.render", null, {
    initializer: function(bindArgs) {
        this.domid = bindArgs["dom"];
        this.ioCount = 0;

        if(dj_undef("action", bindArgs)) {
            this.jsPath = null;
        } else {
            this.jsPath = bindArgs["action"];
            this.ioCount++;
        }

        if(dj_undef("data", bindArgs)) {
            this.jsonPath = null;
        } else {
            this.jsonPath = bindArgs["data"];
            this.ioCount++;
        }

        if(dj_undef("template", bindArgs)) {
            this.templatePath = null;
        } else {
            this.templatePath = bindArgs["template"];
            this.ioCount++;
        }
    },

    process: function(instance) {
        if(this.haveTpl && this.haveData) {
            var result = this.template.process(this.data);
            dojo.byId(this.domid).innerHTML = result;
        }

        if(this.haveTpl && !this.haveData) {
            var result = this.template;
            dojo.byId(this.domid).innerHTML = result;
        }

        if(this.haveAction) {
            dj_eval(this.script);
        }

        this.renderComplete();
    },

    render: function() {
        var __self = this;
        var asynControler = new lotex.system.AsynControler(this.ioCount);
        if(this.templatePath!=null) {
            var tpl = lotex.io.bind({
                url: __self.templatePath,
                error: function(type, data, evt){
                    __self.haveTpl = false;
                    asynControler.remove("tpl");
                    d("tpl can't load. current asynControler.signal is : " + asynControler.signal);
                },
                load: function(type, data, evt) {
                    __self.haveTpl = true;
                    __self.template = data;
                    asynControler.remove("tpl");
                    d("tpl loaded! current asynControler.signal is : " + asynControler.signal);
                    d(__self.templatePath + " : " + __self.template);
                },
                mimetype: "text/plain"
            });
        }


        if(this.jsonPath!=null) {
            var json = lotex.io.bind({
                url: __self.jsonPath,
                error: function(type, data, evt){
                    __self.haveData = false;
                    asynControler.remove("json");
                    d("json can't load. current asynControler.signal is : " + asynControler.signal);
                },
                load: function(type, data, evt) {
                    __self.haveData = true;
                    __self.data = data;
                    asynControler.remove("json");
                    d("json loaded! current asynControler.signal is : " + asynControler.signal);
                    d(__self.jsonPath + " : " + __self.data);
                },
                mimetype: "text/json"
            });
        }
        if(this.jsPath!=null) {
            var cmd = lotex.io.bind({
                url: __self.jsPath,
                error: function(type, data, evt){
                    __self.haveAction = false;
                    asynControler.remove("cmd");
                    d("action can't load. current asynControler.signal is : " + asynControler.signal);
                },
                load: function(type, data, evt) {
                    __self.haveAction = true;
                    __self.script = data;
                    asynControler.remove("cmd");
                    //d("action loaded! current asynControler.signal is : " + asynControler.signal);
                },
                mimetype: "text/plain"
            });
        }
        dojo.event.connect(asynControler, "complete", function() {
            __self.process(__self);
        });
    },

    renderComplete: function() {
    }
});

lotex.renderBind = function(bindArgs) {
    var r = new lotex.render(bindArgs);
    r.render();
    return r;
};